﻿namespace wToggl
{
    using System;

    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;

    public sealed partial class ManualEntryDialog
    {
        public ManualEntryDialog() : this(DateTime.Now.AddMinutes(-15), DateTime.Now)
        {           
        }

        public ManualEntryDialog(DateTime start, DateTime end)
        {
            this.InitializeComponent();

            this.StartDatePicker.Date = start.Date;
            this.EndDatePicker.Date = end.Date;
            this.StartTimePicker.Time = start.TimeOfDay;
            this.EndTimePicker.Time = end.TimeOfDay;
        }

        public DateTime Start { get; private set; }

        public DateTime End { get; private set; }

        private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
            var selectedStart = this.StartDatePicker.Date.Date.Add(this.StartTimePicker.Time);
            var selectedEnd = this.EndDatePicker.Date.Date.Add(this.EndTimePicker.Time);

            if (selectedStart >= selectedEnd)
            {
                args.Cancel = true;
                var flyoutBase = FlyoutBase.GetAttachedFlyout(sender);

                flyoutBase.ShowAt(sender);
                return;
            }

            this.Start = selectedStart;
            this.End = selectedEnd;
        }

        private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
        {
        }
    }
}
